<?php
/**
 * Affichage du formulaire permettant d'ajouter une salle, voir classroomUser.html
 * @global type $babBody 
 */
function displayAddClassroomForm() {
    global $babBody;

    class displayAddClassroomFormClass {

        function displayAddClassroomFormClass() {
            /* LABELS */
            $this->classroomNameLabel = bab_translate("Classroom", $GLOBALS['babAddonFolder']);
            $this->capacityLabel = bab_translate("Capacity", $GLOBALS['babAddonFolder']);
            $this->videoprojectorLabel = bab_translate("Videoprojector", $GLOBALS['babAddonFolder']);
            $this->buildingNameLabel = bab_translate("Building", $GLOBALS['babAddonFolder']);

            /* BUTTON */
            $this->addClassroomButton = bab_translate("Add Classroom", $GLOBALS['babAddonFolder']);
            
            /* NEXTCASSROOM */
            $this->classroomList = unsCalendar_getAllBuilding();
            $this->classroomListLength = count($this->classroomList);
        }

        function nextClassroom() {
            static $i = 0;
            if ($i < $this->classroomListLength) {

                $this->classroom = $this->classroomList[$i];
                $i++;
                return true;
            } else {
                return false;
            }
        }

    }

    $obj = new displayAddClassroomFormClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "classroomUser.html", "addClassroom"));
}

/**
 * Affichage de la liste des salles, voir classroomUser.html
 * @global type $babBody 
 */
function displayListClassroom() {

    global $babBody;

    class displayListClassroomClass {

        function displayListClassroomClass() {
            /* LABELS */
            $this->classroomNameLabel = bab_translate("Classroom", $GLOBALS['babAddonFolder']);
            $this->capacityLabel = bab_translate("Capacity", $GLOBALS['babAddonFolder']);
            $this->videoprojectorLabel = bab_translate("Videoprojector", $GLOBALS['babAddonFolder']);
            
            /* NEXYBUILDING */
            $this->buildingList = unsCalendar_getAllBuilding();
            $this->buildingListLength = count($this->buildingList);
        }

        function nextBuilding() {
            static $k = 0;

            if ($k < $this->buildingListLength) {

                $this->building = $this->buildingList[$k];
                $this->classroomList = unsCalendar_getClassroomForBuilding($this->building['id']);
                $this->classroomListLength = count($this->classroomList);

                $k++;
                return true;
            } else {
                return false;
            }
        }

        function nextClassroom() {
            static $i = 0;
            if ($i < $this->classroomListLength) {

                $this->classroom = $this->classroomList[$i];
                $i++;
                return true;
            } else {
                return false;
            }
        }

    }

    $obj = new displayListClassroomClass();
    $babBody->babecho(bab_printTemplate($obj, $GLOBALS['babAddonHtmlPath'] . "classroomUser.html", "list"));
}

/**
 * Fonction permettant d'ajouter une salle, voir functions/funcClassroom.php
 * @global type $babBody 
 */
function addClassroom($name, $capacity, $buildingID, $videoprojector) {
    if (!empty($_POST['capacityClassroom']) && !empty($_POST['classroomID']) && !empty($_POST['classroomName'])) {

        unsCalendar_addClassroom($name, $capacity, $buildingID, $videoprojector, 1);
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

/**
 * Fonction permettant de supprimer une salle, voir functions/funcClassroom.php
 */
function deleteClassroom($id)
{
    if($id != null)
    {
        echo $id;
        unsCalendar_deleteClassroom($id);
    }   
    
}

/**
 * Fonctions appelées lors de la validation des formulaires d'ajout 
 */
if (isset($_POST['addClassroomSubmit'])) {
    $videoProjector = 0;
    $videoProjector = isset($_POST['videoProjector']) ? '1' : '0';
    addClassroom($_POST['classroomName'], $_POST['capacityClassroom'], $_POST['classroomID'], $videoProjector);
}

/**
 * Affichage des onglets 
 */
global $babBody;
$babBody->addItemMenu("listClassroom", bab_translate("Classroom List", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=classroom&do=listClassroom");
$babBody->addItemMenu("addClassroom", bab_translate("Add Classroom", $GLOBALS['babAddonFolder']), $GLOBALS['babAddonUrl'] . "user&idx=classroom&do=addClassroom");

/**
 * Par defaut on liste les salles
 */
$do = bab_rp('do', 'listClassroom');

switch ($do) {
    case "addClassroom"://Ajout d'une salle, on éxécute la fonction d'affichage du formulaire d'ajout
        //if (bab_isAccessValid('unsDepartement_rights_groups', 1000)) {
        $babBody->title = bab_translate("Add Classroom", $GLOBALS['babAddonFolder']);
        displayAddClassroomForm();
        break;
    // }
    case "listClassroom"://Affichage des salles, on exécute la fonction d'affichage des salles
        $babBody->title = bab_translate("Classrooms List", $GLOBALS['babAddonFolder']);
        displayListClassroom();
        break;
    
    case "deleteClassroom"://Suppression d'une salle, on exécute la fonction de suppression
        deleteClassroom(bab_rp('idClassroom'));
        break;
}
/**
 * Affichage en gras de l'onglet actuel
 */
$babBody->setCurrentItemMenu($do);
?>
